import { Slots } from 'vue'
import { isFunction } from '@/utils/is'
import {TableColumn} from '@/types/table'
import {md5Encrypt} from '@/components/Verifition/src/utils/ase'
import { useCache } from '@/hooks/web/useCache'
import { cloneDeep } from 'lodash-es'

export const getSlot = (slots: Slots, slot = 'default', data?: Recordable) => {
  // Reflect.has 判断一个对象是否存在某个属性
  if (!slots || !Reflect.has(slots, slot)) {
    return null
  }
  if (!isFunction(slots[slot])) {
    console.error(`${slot} is not a function!`)
    return null
  }
  const slotFn = slots[slot]
  if (!slotFn) return null
  return slotFn(data)
}
/**
 *获取表格表头显示缓存key 
 * @param columns 表格全量显示列配置
 * @returns 
 */
export const getTableColumnCacheKey =(columns:TableColumn[])=>{
  const fields=columns.map(v=>v.field)
  fields.sort()
  const key=fields.join(',')
  return md5Encrypt(key)
}
/**
 * 缓存列配置到本地
 * @param columns 表格全量显示列配置
 * @param showColumns 显示的列配置
 */
export const setTableColumnsCache=(columns:TableColumn[],showColumns:TableColumn[])=>{
  const key=getTableColumnCacheKey(columns)
  const { wsCache } = useCache()
  const showColumnsFields=showColumns.map(v=>v.field+"||"+v.width)
  wsCache.set(key,showColumnsFields.join(','))
}
export const clearTableColumnsCache=(columns:TableColumn[])=>{
  const key=getTableColumnCacheKey(columns)
  const { wsCache } = useCache()
  wsCache.delete(key)
}
export const getTableShowColumns=(columns:TableColumn[])=>{
  const cpColumns=cloneDeep(columns)
  const showColumns:TableColumn[]=[]
  const key=getTableColumnCacheKey(columns)
  const { wsCache } = useCache()
  const showColumnsFieldString=wsCache.get(key)
  if(showColumnsFieldString&&showColumnsFieldString!=''){
    const showColumnsFields=showColumnsFieldString.split(',')
    if(showColumnsFields&&showColumnsFields.length>0){
      showColumnsFields.forEach(fieldItem=>{
        const [field,width]=fieldItem.split('||')
        const column=cpColumns.find(v=>v.field==field)
        if(column){
          column.width=width?Number(width):undefined
          showColumns.push(column)
        }
      })
      return showColumns
    }
  }
  //后续加上原始列显示逻辑
  return cpColumns.filter(v=>!v.isColumnHidden)
}
/**
* 获取表格列的按排序列
*
* @param columns 表格列数组
* @returns 返回排序键
*/
export const getTableColumnsSortColumns=(columns:TableColumn[])=>{
  const key=getTableColumnCacheKey(columns)
  const { wsCache } = useCache()
  const showColumnsFieldString=wsCache.get(key)
  const sortColumnKeys:string[]=[]
  if(showColumnsFieldString&&showColumnsFieldString!=''){
    const showColumnsFields=showColumnsFieldString.split(',')
    showColumnsFields.forEach(fieldItem=>{
      const [field,width]=fieldItem.split('||')
      sortColumnKeys.push(field)
    })
  }
  //显示的排在前面，隐藏的在后面
  columns.sort((a, b) => {
    const aIndex = sortColumnKeys.indexOf(a.field);
    const bIndex = sortColumnKeys.indexOf(b.field);
    if(a.field=='action') return 0;
    if(aIndex === -1) return 1;
    if(bIndex === -1) return -1;
    return aIndex - bIndex;
  })
  return columns
}